package com.ksjourney;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import mars.game.core.Config;
import mars.game.core.MarsRuntime;
import mars.game.core.MarsScreen;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class GamePlay implements MarsScreen, Config
{
	
	// //UI状态
	// public static final int LOAD_RES = 0;
	//	
	public static final int MAIN = 1;
	//	
	// public static final int HELP = 2;
	//	
	// public static final int ABOUT = 3;
	//	
	// public static final int SET = 4;
	//	
	// public static final int LOAD_GAME = 5;
	//	
	// public static final String STR_LOAD = "Loading....";
	
	public static final int BG_SPEED_1 = 2;
	
	public static final int BG_SPEED_2 = 6;
	
	int stage;
	
	int miCount = 0;
	
	int bg_x1 = 0;
	
	int bg_x2 = 0;
	
	int state;
	
	Bitmap bg1;
	
	Bitmap bg2;
	
	private int balloonNums;
	
	private int enemyNums;
	
	private ArrayList<Balloon> balloons;
	
	private ArrayList<Enemy> enemy;
	
	private Kimo kimo;
	
	private GameUI gui;
	
	public GamePlay(GameUI gui)
	{

		this.gui = gui;
	}
	
	public void initStage(int stage)
	{

		switch (stage)
		{
			case 1:
				balloonNums = 10;
				enemyNums = 5;
				break;
			case 2:
				balloonNums = 8;
				enemyNums = 8;
			case 3:
				balloonNums = 5;
				enemyNums = 10;
				break;
			default:
				break;
		}
		// 初始化气球
		balloons = new ArrayList<Balloon>();
		for (int i = 0; i < balloonNums; i++)
		{
			balloons.add(Balloon.markBalloon());
		}
		// 初始化怪
		enemy = new ArrayList<Enemy>();
		for (int i = 0; i < enemyNums; i++)
		{
			enemy.add(Enemy.markEnemy());
		}
		// 初始化主角精灵
		kimo = Kimo.markKimo();
	}
	
	
	
	private void loadRes(int stage)
	{

		int bgResId = R.drawable.stage1_bg;
		int bg1ResId = R.drawable.stage1_bg2;
		switch (stage)
		{
			case 1:
				bgResId = R.drawable.stage1_bg;
				bg1ResId = R.drawable.stage1_bg2;
				break;
			case 2:
				bgResId = R.drawable.stage2_bg;
				bg1ResId = R.drawable.stage2_bg2;
				break;
			case 3:
				bgResId = R.drawable.stage3_bg;
				bg1ResId = R.drawable.stage3_bg2;
				break;
			
			default:
				break;
		}
		
		// 加载图片
		bg1 = MarsRuntime.loadBitmap(bgResId);
		bg2 = MarsRuntime.loadBitmap(bg1ResId);
		// 加载声音
	}
	
	// private void drawRES(Canvas canvas)
	// {
	// Paint mPaint = new Paint();
	// mPaint.setColor(Color.RED);
	// canvas.drawText(STR_LOAD.substring(0, end), SCREEN_WIDTH / 2,
	// SCREEN_HEIGHT / 2, mPaint);
	// }
	
	// private void enterPlay()
	// {
	// GamePlay gp = new GamePlay();
	// MarsRuntime.getRuntime().changeScreen(gp, GamePlay.MAIN, null);
	// }
	
	@Override
	public void draw(Canvas canvas)
	{

		canvas.clipRect(Config.SCREEN_RECT);
		
		switch (state)
		{
			case MAIN:
				drawBg(canvas);
				// 绘制气球
				System.out.println(" 绘制气球");
				for (int i = 0; i < balloons.size(); i++)
				{
					balloons.get(i).draw(canvas);
				}
				System.out.println("绘制怪");
				// 绘制怪
				for (int i = 0; i < enemy.size(); i++)
				{
					enemy.get(i).draw(canvas);
				}
				System.out.println("绘制精灵");
				// 绘制精灵
				kimo.draw(canvas);
				break;
		}
		
		// canvas.drawBitmap(bg2, bg_x1, 0, null);
		// bg_x2 = bg1.getWidth();
		// canvas.drawBitmap(bg2, bg_x2, 0, null);
		
	}
	
	private void drawBg(Canvas canvas)
	{

		// 第一层背景
		int x1 = bg_x1 + bg1.getWidth();
		canvas.drawBitmap(bg1, bg_x1, 0, null);
		if (x1 <= Config.SCREEN_WIDTH)
		{
			canvas.drawBitmap(bg1, x1, 0, null);
		}
		// 第二层背景
		int x2 = bg_x2 + bg2.getWidth();
		canvas.drawBitmap(bg2, bg_x2, 0, null);
		if (x2 <= Config.SCREEN_WIDTH)
		{
			canvas.drawBitmap(bg2, x2, 0, null);
		}
		
	}
	
	// }
	
	@Override
	public void tick()
	{

		switch (state)
		{
			case MAIN:
				tickMain();
				// Kimo的tick()
				kimo.tick();
				for (Iterator<Balloon> it = balloons.iterator(); it.hasNext();)
				{
					Balloon ba = it.next();
					ba.tick();
					if (!ba.isInScreen())
					{
						it.remove();
					}
				}
				
				// for (Balloon ba : balloons)
				// {
				// ba.tick();
				//					
				// if (!ba.isInScreen())
				// {
				// // balloons.remove(ba);
				// balloons.iterator().remove();
				// }
				// }
				
				for (Iterator<Enemy> it = enemy.iterator(); it.hasNext();)
				{
					Enemy en  = it.next();
					en.tick();
					if (!en.isInScreen())
					{
						it.remove();
					}
				}
				
				//				
				// for (Enemy en : enemy)
				// {
				// en.tick();
				// if (!en.isInScreen())
				// {
				// // enemy.remove(en);
				// enemy.iterator().remove();
				// }
				// }
				
				break;
		}
		
	}
	
	private void tickMain()
	{

		// 第一层
		if (bg_x1 <= -bg1.getWidth() + BG_SPEED_1)
		{
			bg_x1 = 0;
		}
		else
		{
			bg_x1 -= BG_SPEED_1;
		}
		// 第二层
		if (bg_x2 <= -bg2.getWidth() + BG_SPEED_2)
		{
			bg_x2 = 0;
		}
		else
		{
			bg_x2 -= BG_SPEED_2;
		}
	}
	
	@Override
	public void focusIn(int entryPoint, Object... args)
	{

		state = entryPoint;
		stage = ((Integer) args[0]).intValue();
		loadRes(stage);
		initStage(stage);
	}
	
	@Override
	public void focusOut()
	{

	}
	
	@Override
	public void releaseRes()
	{

		this.enemy = null;
		this.balloons = null;
		this.kimo = null;
		this.bg1 = null;
		this.bg2 = null;
		
	}
	
	@Override
	public void onKeyDown(int keyCode, KeyEvent event)
	{
		
	}
	
	@Override
	public void onKeyUp(int keyCode, KeyEvent event)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			this.back();
		}
		
	}
	
	@Override
	public void onTouchEvent(MotionEvent event)
	{
		
		// 点中气球事件
		for (int i = 0; i < balloons.size(); i++)
		{
			balloons.get(i).onTouchEvent(event);
		}
		// 点中怪事件
		for (int i = 0; i < enemy.size(); i++)
		{
			enemy.get(i).onTouchEvent(event);
		}
	}
	
	private void back()
	{

		if (state == MAIN)
		{
			MarsRuntime.getRuntime().changeScreen(gui, GameUI.MAIN);
			
		}
		else
		{
			state = MAIN;
		}
	}
	
}
